package parsing.productions;

import parsing.InvalidSyntaxException;


public class AddOp extends MutableNode {
	// Invariant for children: empty.
	
	/**+*/
	public static final int PLUS  = 1;
	/**-*/
	public static final int MINUS = 2;

	private int type;
	
	/**
	 * Constructor: creates an instance of AddOp that is either PLUS or MINUS.
	 * @param operation type of AddOp.
	 * Checks: operation is PLUS or MINUS.
	 */
	public AddOp(int operation) {
		if (!(operation == PLUS || operation == MINUS)) {
			throw new InvalidSyntaxException("The only types this AddOp can have are PLUS or MINUS");
		}
		type= operation;
	}
	
	public int getType(){
		return type;
	}
	
	@Override
	public int size() {
		return 1;
	}

	@Override
	public void prettyPrint(StringBuffer sb) {
		switch(type){
		case PLUS: sb.append("+ "); break;
		case MINUS: sb.append("- "); break;
		}
	}

	/** Switches type of this AddOp. */
	@Override
	public void mutate(Program genome) {
		if (type == PLUS){
			type = MINUS;
//			System.out.println("a + was changed to a -");
		}
		else{
			type = PLUS;
//			System.out.println("a - was changed to a +");
		}
	}

	@Override
	public Node duplicate() {
		return new AddOp(type);
	}

	/**
	 * Checks if this AddOp is the same as n
	 * @param n the node to check against
	 * @return true if n is a AddOp and its type is the same as this one's
	 */
	@Override
	public boolean equals(Node n){
		if(!(n instanceof AddOp))
			return false;
		AddOp aNode = (AddOp) n;
		if(type != aNode.type)
			return false;
		return true;
	}
}
